/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.jbfilter.core.FilterComponent;
import org.jbfilter.core.FilterComponentContainer;

class _FilterComponentContainerSupport<E>
	implements FilterComponentContainer<E>{

	// The components mapped to their id's.
	private Map<String, FilterComponent<E>> filterComponentsById = new LinkedHashMap<String, FilterComponent<E>>();
	// same but read-only
	private Map<String, FilterComponent<E>> filterComponentsByIdReadOnly = Collections.unmodifiableMap(filterComponentsById);
	// null === no restriction : accepts any filter component, otherwise specify supported classes
	private Set<Class<?>> classRestriction = null;
	
	@Override
	public void addFilterComponent(FilterComponent<E> fcomp) {
		if (fcomp == null) {
			throw new NullPointerException(
					String.format("Cannot add null values to %s.", FilterComponentContainer.class.getSimpleName()));
		}
		checkClass(fcomp);
		String key = fcomp.getId();
		if (key == null) {
			key = getClass().getName() + (filterComponentsById.size() + 1);
		}
		// 2 filter components cannot have the same key
		if (filterComponentsById.containsKey(key)) {
			throw new IllegalArgumentException("There is already a filter component registered with the key : " + key);
		}
		filterComponentsById.put(key, fcomp);
	}
	@Override
	public FilterComponent<E> getFilterComponent(String id) {
		return filterComponentsById.get(id);
	}
	@Override
	public Collection<FilterComponent<E>> getFilterComponents() {
		return filterComponentsById.values();
	}
	@Override
	public Map<String, FilterComponent<E>> getFilterComponentsById() {
		return filterComponentsByIdReadOnly;
	}
	public void addClassRestriction(Class<?> acceptedClass) {
		if (classRestriction == null) {
			classRestriction = new HashSet<Class<?>>();
		}
		classRestriction.add(acceptedClass);
	}
	private void checkClass(FilterComponent<E> fcomp) {
		if (classRestriction == null) {
			return;
		}
		boolean ok = false;
		for (Class<?> clazz : classRestriction) {
			ok = clazz.isInstance(fcomp);
			if (ok) {
				break;
			}
		}
		if (!ok) {
			throw new IllegalArgumentException(String.format("This container accepts only %s.", classRestriction.toString()));
		}
	}
}
